﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DemoData;
using DemoData.Model;
using Hallenberg.Core;
using NUnit.Framework;

namespace DemoDataTest.Providers
{
    [TestFixture]
    public class ProductProviderTest
    {
        [Test]
        public void DemoData_Providers_ProductProvider_Get_Filter_On_Name()
        {
            Repository.PopulateDemoData();
            foreach (Product product in Repository.Products.Get().Where(p => p.ProductName.IndexOf("red", StringComparison.OrdinalIgnoreCase) != -1))
            {
                Assert.AreNotEqual(-1, product.ProductName.IndexOf("red", StringComparison.OrdinalIgnoreCase));
            }
        }

        [Test]
        public void DemoData_Providers_ProductProvider_Save()
        {
            int assignedId = -1;
            Product changedProduct = null;
            Product product = new Product("My Product", 100d);

            Repository.Products.Save(product);
            assignedId = product.Id;

            product.ProductName = "My Changed Product";
            Repository.Products.Save(product);

            changedProduct = Repository.Products.Get().FirstOrDefault(p => p.ProductName == product.ProductName);

            Assert.AreNotEqual(-1, assignedId);
            Assert.IsNotNull(changedProduct);
            Assert.AreEqual(product.ProductName, changedProduct.ProductName);
        }

        [Test]
        public void DemoData_Providers_ProductProvider_Save_Pass_Null()
        {
            var exception = Assert.Throws<ArgumentNullException>(() => Repository.Products.Save(null));

            Assert.That(exception.ParamName, Is.EqualTo("value"));
        }

        [Test]
        public void DemoData_Providers_ProductProvider_Save_Pass_Invalid_Product()
        {
            var exception = Assert.Throws<ValidationException>(() => Repository.Products.Save(new Product("Super Product", 0) { ProductName = string.Empty }));
            Assert.That(exception, Is.Not.Null);
        }
    }
}
